﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Windows.Forms;

namespace SIMc
{
    public class XMLHistory
    {
        XmlDocument xmlDoc;
        string path;

        public XMLHistory(string appPath)
        {
            this.path = appPath + "history.xml";
            xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(path);
            }
            catch (Exception e)
            {
                createUltraHistory();
            }
        }

        public void createHistory()
        {
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "no");

            XmlElement rootNode = xmlDoc.CreateElement("History");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);
        }

        public void createUltraHistory()
        {
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "no");

            XmlElement rootNode = xmlDoc.CreateElement("History");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            for (int i = 1; i < 7; i++) {
                addProfile(i);
            }
        }

        public bool addProfile(int profileID)
        {
            XmlElement profileElem = xmlDoc.CreateElement("profile");
            xmlDoc.DocumentElement.AppendChild(profileElem);
            XmlAttribute IDAttr = xmlDoc.CreateAttribute("ID");
            IDAttr.Value = profileID.ToString();
            profileElem.Attributes.Append(IDAttr);

            xmlDoc.Save(path);
            return true;
        }

        public bool addContact(int profileID, string uri)
        {
            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if ((int.Parse(lNode.Attributes.Item(0).Value)).Equals(profileID))
                {
                    //nasiel profileID
                    XmlElement contactElem = xmlDoc.CreateElement("contact");
                    XmlAttribute uriAttr = xmlDoc.CreateAttribute("uri");
                    uriAttr.Value = uri;
                    contactElem.Attributes.Append(uriAttr);

                    lNode.AppendChild(contactElem);

                    xmlDoc.Save(path);
                    return true;
                }
            }
            return false;
        }

        public bool addPost(int profileID, string uri, string header, string post)
        {
            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if ((int.Parse(lNode.Attributes.Item(0).Value)).Equals(profileID))
                {
                    //nasiel profileID
                    foreach (XmlNode contactNode in lNode.ChildNodes)
                    {
                        if (contactNode.Attributes.Item(0).Value.Equals(uri))
                        {
                            //nasiel contact s uri
                            XmlElement postElem = xmlDoc.CreateElement("post");
                            XmlAttribute headerAttr = xmlDoc.CreateAttribute("header");
                            headerAttr.Value = header;
                            postElem.Attributes.Append(headerAttr);
                            postElem.InnerText = post;

                            contactNode.AppendChild(postElem);
                            xmlDoc.Save(path);
                            return true;
                        }
                    }
                    //nenasiel dane uri
                    addContact(profileID, uri);
                    addPost(profileID, uri, header, post);
                }
            }
            return false;
        }

        internal ArrayList getContactNames(int profileID)
        {
            ArrayList zoznam = new ArrayList();

            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if ((int.Parse(lNode.Attributes.Item(0).Value)).Equals(profileID))
                {
                    //nasiel profileID
                    foreach (XmlNode contactNode in lNode.ChildNodes)
                    {
                        zoznam.Add(contactNode.Attributes[0].Value);
                    }
                    return zoznam;
                }

            }
            return null;
        }

        internal TextBox fillTextBox(int profileID, string contact, TextBox textBox) {
            textBox.Text = "";
            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if ((int.Parse(lNode.Attributes.Item(0).Value)).Equals(profileID))
                {
                    //nasiel profileID
                    foreach (XmlNode contactNode in lNode.ChildNodes)
                    {
                        if (contactNode.Attributes.Item(0).Value.Equals(contact))
                        {
                            foreach (XmlNode postNode in contactNode.ChildNodes)
                            {
                                textBox.Text += postNode.Attributes[0].Value+ "\r\n";
                                textBox.Text += postNode.InnerText + "\r\n";
                            }
                        }
                    }
                }
            }
            return textBox;
        }

    }
}
